package com.yikaiye.android.yikaiye.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ItemServiceDetailsCorrelationServiceBinding;
import com.yikaiye.android.yikaiye.model.ServiceRecordsModel;
import com.yikaiye.android.yikaiye.ui.activity.service.ServiceDetailsActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.UiUtils;

public class CorrelationServiceAdapter extends BaseQuickAdapter<ServiceRecordsModel.ServiceModel, DataBindingHolder<ItemServiceDetailsCorrelationServiceBinding>> {

    private final Context context;

    public CorrelationServiceAdapter(Context context) {
        this.context = context;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemServiceDetailsCorrelationServiceBinding> itemBindingDataBindingHolder, int i, @Nullable ServiceRecordsModel.ServiceModel serviceModel) {
        ItemServiceDetailsCorrelationServiceBinding binding = itemBindingDataBindingHolder.getBinding();
        if (serviceModel != null) {
            binding.serviceNameTv.setText(CommonUtil.checkText(serviceModel.getServiceName()));
            binding.serviceAttTv.setText(CommonUtil.checkText(serviceModel.getServiceAttr()));
            UiUtils.setServiceStatus(serviceModel.getStatus(), binding.serviceStatusTv, context);
            binding.line.setVisibility(getItemCount() - 1 == i ? View.INVISIBLE : View.VISIBLE);
            itemBindingDataBindingHolder.itemView.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.ID_KEY, serviceModel.getId());
                bundle.putString(Constant.FROM_KEY, Constant.service);
                JumpIntent.jump((Activity) context, ServiceDetailsActivity.class, bundle, true);
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemServiceDetailsCorrelationServiceBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_service_details_correlation_service, viewGroup);
    }

}
